package com.Reporte_Consultas;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.Generales.R;
import com.JSON.JSONParser_Final;


import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class RC_3_Muestra_Eventos extends Activity{
	

	private String idSubtipo;	
	public String idPromotor;
	
	public GridView gridviewSubTipos;
	ArrayList<evento> arrayEvento = new ArrayList();
	
	int yInicial[] = {0,0};
	double xInicial[] = {0,0};
	private String nombreUser;
	
	
	public void onCreate(Bundle savedInstanceState) {
		
		nombreUser = this.getIntent().getExtras().getString("nombreUser");
		idSubtipo= this.getIntent().getExtras().getString("idSubtipo");
		idPromotor = this.getIntent().getExtras().getString("idPromotor");
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.re_3_muestra_eventos);
        
		Resources res = getResources();
		Drawable myImage = res.getDrawable(R.drawable.background2);		
		ActionBar actionBar = this.getActionBar();
		actionBar.hide();
		
		gridviewSubTipos = (GridView)findViewById(R.id.grid_eventos);
	
		arrayEvento = getEventos(idSubtipo);
		
		if (arrayEvento.size()==0) {
        	Toast.makeText(this, "No existe ningún Evento. Retroceder y elegir otro Subtipo.", Toast.LENGTH_SHORT).show();
        	
        }else{
        	ImageAdapter_Eventos adapter = new ImageAdapter_Eventos(arrayEvento, this);	
    		
    		gridviewSubTipos.setAdapter(adapter);
    		gridviewSubTipos.setOnItemClickListener(new OnItemClickListener()
    	    {
    			@Override
    			public void onItemClick(AdapterView<?> adapter, View v, int position,long id) {
    				// TODO Auto-generated method stub
    				evento evento = (evento)adapter.getItemAtPosition(position);				
    				Intent intent = new Intent(RC_3_Muestra_Eventos.this,Reporte_Consultas.class);
    		    	intent.putExtra("idEvento",String.valueOf(evento.getIdEvento()));
    		    	intent.putExtra("nombreUser",nombreUser);
    		    	startActivity(intent);
    			}	    	
    	    });
        	
        }   
		
      }


	
	private ArrayList<evento> getEventos(String idSubtipo2) {
		ArrayList<evento> array = new ArrayList<evento>();
		JSONArray  jArray = null;
		try {
			jArray = (new JSONParser_Final().execute("http://200.16.7.112:3000/reports_management/report/evento/"+idSubtipo2+".json")).get();
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		} catch (ExecutionException e) {
			
			e.printStackTrace();
		}
		
		try {
			for(int i = 0; i < jArray.length(); i++){
				 JSONObject jsonItem = jArray.getJSONObject(i);
		         JSONObject jsonItem1 = jsonItem.getJSONObject("cubo_evento");
		         
		         int idEvento = jsonItem1.getInt("id");
		         String url = jsonItem1.getString("urlEvento");
		         
		         evento ev = new evento(idEvento, url);
		         array.add(ev);
			}
		}catch (JSONException e) {
		     e.printStackTrace();
		 }
		return array;
	
	}	
	
	
}
